package com.hethongviet.preventing.page;

import group.pals.android.lib.ui.filechooser.FileChooserActivity;
import group.pals.android.lib.ui.filechooser.io.LocalFile;
import group.pals.android.lib.ui.filechooser.services.IFileProvider;

import java.io.File;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;

import com.hethongviet.preventingmachine.Constants;
import com.hethongviet.preventingmachine.R;
import com.hethongviet.preventingmachine.Utils;

public class PageChangeSoundActivity extends Activity
{
	private EditText editTextPath = null;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		Utils.prepareWindow(this);

		setContentView(com.hethongviet.preventer.admin.R.layout.page_change_sound);

		final String soundPath = getIntent().getStringExtra(
				Constants.EXTRA_SOUND_PATH);

		editTextPath = (EditText) findViewById(com.hethongviet.preventer.admin.R.id.change_path_edit_text);
		editTextPath.setText(soundPath == null ? "" : soundPath);

		final Button buttonBrowse = (Button) findViewById(com.hethongviet.preventer.admin.R.id.change_button_sound);

		buttonBrowse.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(PageChangeSoundActivity.this,
						FileChooserActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("title", getString(R.string.app_name));

				bundle.putSerializable(
						IFileProvider.FilterMode.class.getName(),
						IFileProvider.FilterMode.FilesAndDirectories);

				intent.putExtras(bundle);
				startActivityForResult(intent, Constants.REQUEST_CHANGE_SOUND);
			}
		});

		final CheckBox checkBoxDefault = (CheckBox) findViewById(com.hethongviet.preventer.admin.R.id.change_sound_checkbox);
		checkBoxDefault.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1)
			{
				if (checkBoxDefault.isChecked())
				{
					buttonBrowse.setClickable(false);
					buttonBrowse.setTextColor(Color.GRAY);

					editTextPath.setEnabled(false);
					editTextPath.setClickable(false);
				}
				else
				{
					buttonBrowse.setClickable(true);
					buttonBrowse.setTextColor(Color.BLACK);

					editTextPath.setEnabled(true);
					editTextPath.setClickable(true);
				}
			}
		});

		final Button buttonAccept = (Button) findViewById(R.id.change_button_accept);
		buttonAccept.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent();
				if (checkBoxDefault.isChecked())
				{
					intent.putExtra(Constants.EXTRA_SOUND_PATH, "");
				}
				else
				{
					intent.putExtra(Constants.EXTRA_SOUND_PATH,
							editTextPath.getText().toString());
				}
				setResult(RESULT_OK, intent);
				finish();
			}
		});

		final Button buttonCancel = (Button) findViewById(R.id.change_button_cancel);
		buttonCancel.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				setResult(RESULT_CANCELED);
				finish();
			}
		});

	}

	@SuppressWarnings("unchecked")
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		switch (requestCode)
		{
			case Constants.REQUEST_CHANGE_SOUND:
				if (resultCode == RESULT_OK)
				{
					List<LocalFile> localFiles = (List<LocalFile>) data.getSerializableExtra(FileChooserActivity.Results);
					File file = localFiles.get(0);

					editTextPath.setText(file.getAbsolutePath());
				}
				break;

		}

	}

}
